package com.neo.showCode;

import com.neo.showCode.domain.CodeCache;
import com.neo.showCode.domain.CodeParam;
import com.neo.showCode.domain.CodeResult;
import com.neo.showCode.domain.MesPackage;

import java.util.function.Consumer;

/**
 * @Description: 展码
 * @Author: panrui
 * @Date: 2023/6/28 17:28
 */
public class BaseCodeGen {
    // 缓存的码规则
    private CodeCache codeCache;
    // 入参参数
    private CodeParam codeParam;
    // 消息包
    private MesPackage mesPackage;
    // 返回结果
    private CodeResult codeResult;


    public BaseCodeGen(CodeParam codeParam){
        this.codeParam = codeParam;
        // 从缓存中获取码规则
        this.codeCache = findCodeCache(codeParam);
        this.codeResult = new CodeResult();
        // 初始化消息包
        this.mesPackage = new MesPackage(this.codeParam,this.codeCache);
    }

    /**
     * 查询码规则
     * @param codeParam
     * @return
     */
    private CodeCache findCodeCache(CodeParam codeParam) {
        return new CodeCache();
    }

    /**
     * 处理码缓存
     * @param function
     * @return
     */
    public BaseCodeGen dealCodeCache(Consumer<CodeCache> function) {
        function.accept(this.codeCache);
        return this;
    }
    /**
     * 处理入参
     * @param function
     * @return
     */
    public BaseCodeGen dealCodeParam(Consumer<CodeParam> function) {
        function.accept(this.codeParam);
        return this;
    }

    /**
     * 处理消息包
     * @param function
     * @return
     */
    public BaseCodeGen dealMsgPackage(Consumer<MesPackage> function) {
        function.accept(this.mesPackage);
        return this;
    }

    /**
     * 处理返回结果
     * @param function
     * @return
     */
    public BaseCodeGen dealCodeResult(Consumer<CodeResult> function) {
        function.accept(this.codeResult);
        return this;
    }

    /**
     * 生成码串
     * @return
     */
    public CodeResult build(){

        String content = this.mesPackage.pack();
        this.codeResult.put("content", content);
        return this.codeResult;
    }


}
